/*
 * Author:   W.SH
 */
package com.wgg.crawler.util;

import java.util.LinkedList;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 *  @author W.SH
 */
@SuppressWarnings("serial")
public class LinksList extends LinkedList<String> {

    public void getLinkByA(Document document, RegexRule regexRule){
        Elements elements = document.select("a[href]");
        for (Element element : elements){
            String url = element.attr("abs:href");   //这里使用abs:开头 以便获取绝对路径
            if ((url.length() > 6) && (regexRule.satisfy(url))){
                add(url);
            }
        }
    }

    public void getLinkByA(Document document, String cssQuery){
        Elements elements = document.select("a[href]");

        if (cssQuery != null && cssQuery.length() > 0){
            elements  = document.select(cssQuery);
        }

        for (Element element : elements){
            String url = element.attr("abs:href");
            if (url.length() > 6) {
                add(url);
            }
        }
    }

    public void getLinkByA(Document document, String cssQuery, RegexRule regexRule){
        Elements elements = document.select("a[href]").select(cssQuery);
        for (Element element : elements){
            String url = element.attr("abs:href");
            if ((url.length() > 6) && (regexRule.satisfy(url))){
                add(url);
            }
        }
    }

    public void getLinkByImg(Document document, String cssQuery){
        Elements elements = document.select("img[src]");

        if (cssQuery != null && cssQuery.length() > 0){
            elements  = document.select(cssQuery);
        }

        for (Element element : elements){
            String url = element.attr("abs:src");
            if (url.length() > 6) {
                add(url);
            }
        }
    }
}
